package com.travelsky.airchina.boarding.web;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.travelsky.airchina.boarding.common.ConstType;
import com.travelsky.airchina.boarding.domain.News;
import com.travelsky.airchina.boarding.domain.NewsColumn;
import com.travelsky.airchina.boarding.dto.NewsSearchCondition;
import com.travelsky.airchina.boarding.dto.NewsView;
import com.travelsky.airchina.boarding.service.NewsColumnService;
import com.travelsky.airchina.boarding.service.NewsService;
import com.travelsky.airchina.boarding.util.PageUtil;
@Controller
public class FrontController {
	@Autowired
	private NewsService newsService;
	@Autowired
	private NewsColumnService newsColumnService;
	private int perPageRow = 18;//分页时每页的新闻条数
	/**
	 * 查看新闻信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/news/{id}/*", method=RequestMethod.GET)
	public String getDetail(@PathVariable Long id, Model model) {
		News news = newsService.getNews(id);
		model.addAttribute(news);
		if(news.getNewsColumn()!=null){
			List<NewsColumn> list = new ArrayList<NewsColumn>();
			List<NewsColumn> listTemp = newsColumnService.findAllParentNewsColumnsByChildId(news.getNewsColumn().getId());
			if(listTemp!=null && listTemp.size()>0){
				list.addAll(listTemp);
			}
			list.add(news.getNewsColumn());
			model.addAttribute("list",list);
		}
		return "front/detail";
	}
	/**
	 * 查看新闻信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/news/video/{id}", method=RequestMethod.GET)
	public String getVideoDetail(@PathVariable Long id, ModelMap model) {
		News video = newsService.getNews(id);
		model.addAttribute("video",video);
		if(video.getNewsColumn()!=null){
			List<NewsColumn> list = new ArrayList<NewsColumn>();
			List<NewsColumn> listTemp = newsColumnService.findAllParentNewsColumnsByChildId(video.getNewsColumn().getId());
			if(listTemp!=null && listTemp.size()>0){
				list.addAll(listTemp);
			}
			list.add(video.getNewsColumn());
			model.addAttribute("list",list);
		}
		model.addAttribute("FLV_VIDEO_PATH",ConstType.FLV_VIDEO_PATH);
		return "front/videoDetail";
	}
	/**
	 * 查看新闻信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="index", method=RequestMethod.GET)
	public String index(ModelMap model) {
		List<NewsView> xinwenfabuList = newsService.findLastedNewsListByNewsColumnCode("xinwenfabu",9);
//		List<NewsView> zhengcefaguiList = newsService.findLastedNewsListByNewsColumnCode("zhengcefagui",9);
//		List<NewsView> buweidongtaiList = newsService.findLastedNewsListByNewsColumnCode("buweidongtai",9);
//		List<NewsView> zhongxingonggaoList = newsService.findLastedNewsListByNewsColumnCode("zhongxingonggao",9);
//		List<NewsView> xinnongcunzhuantiList = newsService.findLastedNewsListByNewsColumnCode("xinnongcunzhuanti",9);
//		List<NewsView> shifanjidiList = newsService.findLastedNewsListByNewsColumnCode("shifanjidi",9);
//		List<NewsView> zhuanjiaguwenList = newsService.findLastedNewsListByNewsColumnCode("zhuanjiaguwen",9);
//		List<NewsView> zhaoshangzhinanList = newsService.findLastedNewsListByNewsColumnCode("zhaoshangzhinan",9);

		//List<NewsView> videoList = newsService.findLastedVideoListByNewsColumnCode("shipinzhongxin",1);
		List<NewsView> videoList = newsService.findLastedVideoList(1);
		model.addAttribute("xinwenfabuList", xinwenfabuList);
		if(videoList!=null && videoList.size()>0){
			model.addAttribute("video", videoList.get(0));
		}
		model.addAttribute("FLV_VIDEO_PATH",ConstType.FLV_VIDEO_PATH);
//		model.addAttribute("zhengcefaguiList", zhengcefaguiList);
//		model.addAttribute("buweidongtaiList", buweidongtaiList);
//		model.addAttribute("zhongxingonggaoList", zhongxingonggaoList);
//		model.addAttribute("xinnongcunzhuantiList", xinnongcunzhuantiList);
//		model.addAttribute("shifanjidiList", shifanjidiList);
//		model.addAttribute("zhuanjiaguwenList", zhuanjiaguwenList);
//		model.addAttribute("zhaoshangzhinanList", zhaoshangzhinanList);
		return "front/index";
	}
	/**
	 * 根据栏目主键获得栏目下的新闻列表
	 * @param columnId 栏目主键
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/news/column/{columnId}/{columnName}", method=RequestMethod.GET)
	public String listByColumnId(@PathVariable Long columnId,@PathVariable String columnName, Model model) {
		PageUtil pageUtil = new PageUtil();
		pageUtil.setPerPageRow(perPageRow);
		List<NewsView> list = newsService.findNewsPageByColumnId(pageUtil, columnId);
		model.addAttribute("list", list);
		model.addAttribute("columnId", columnId);
		model.addAttribute("columnName", columnName);
		model.addAttribute("pageUtil", pageUtil);
		return "front/listByColumnId";
	}
	/**
	 * 在得栏目下的新闻列表页面中的分页操作
	 * @param columnId 栏目主键
	 * @param curPage 搜索的当前页
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/news/column/page", method=RequestMethod.POST)
	public String pageListByColumnId(@RequestParam Long columnId,@RequestParam String columnName,@RequestParam int curPage, Model model) {
		PageUtil pageUtil = new PageUtil();
		pageUtil.setCurPage(curPage);
		pageUtil.setPerPageRow(perPageRow);
		List<NewsView> list = newsService.findNewsPageByColumnId(pageUtil, columnId);
		model.addAttribute("list", list);
		model.addAttribute("columnId", columnId);
		model.addAttribute("columnName", columnName);
		model.addAttribute("pageUtil", pageUtil);
		return "front/listByColumnId";
	}
	/**
	 * 根据栏目编码获得栏目下的新闻列表
	 * @param columnCode 栏目编码
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/news/columncode/{columnCode}/{columnName}", method=RequestMethod.GET)
	public String listByColumnCode(@PathVariable String columnCode,@PathVariable String columnName, Model model) {
		PageUtil pageUtil = new PageUtil();
		pageUtil.setPerPageRow(perPageRow);
		List<NewsView> list = newsService.findNewsPageByColumnCode(pageUtil, columnCode);
		model.addAttribute("list", list);
		model.addAttribute("columnCode", columnCode);
		model.addAttribute("columnName", columnName);
		model.addAttribute("pageUtil", pageUtil);
		return "front/listByColumnCode";
	}
	/**
	 * 在得栏目下的新闻列表页面中的分页操作
	 * @param columnCode 栏目编码
	 * @param curPage 搜索的当前页
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/news/columncode/page", method=RequestMethod.POST)
	public String pageListByColumnCode(@RequestParam String columnCode,@RequestParam String columnName,@RequestParam int curPage, Model model) {
		PageUtil pageUtil = new PageUtil();
		pageUtil.setCurPage(curPage);
		pageUtil.setPerPageRow(perPageRow);
		List<NewsView> list = newsService.findNewsPageByColumnCode(pageUtil, columnCode);
		model.addAttribute("list", list);
		model.addAttribute("columnCode", columnCode);
		model.addAttribute("columnName", columnName);
		model.addAttribute("pageUtil", pageUtil);
		return "front/listByColumnCode";
	}
	/**
	 * 搜索新闻
	 * @param searchKey 搜索的关键字
	 * @param curPage 搜索的当前页
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/search/news", method=RequestMethod.POST)
	public String search(@RequestParam String searchKey,@RequestParam int curPage, Model model) {
		PageUtil pageUtil = new PageUtil();
		pageUtil.setCurPage(curPage);
		pageUtil.setPerPageRow(perPageRow);
		NewsSearchCondition newsSearchCondition = new NewsSearchCondition();
		newsSearchCondition.setTitle(searchKey);
		List<NewsView> list = newsService.findNewsPageByCondition(pageUtil, newsSearchCondition);
		model.addAttribute("list", list);
		model.addAttribute("pageUtil", pageUtil);
		model.addAttribute("searchKey", searchKey);
		return "front/searchList";
	}
}
